
clear
set more off


capture confirm file "Main_Results/"
if _rc mkdir "Main_Results/"

capture confirm file "Main_Results/Fig3/"
if _rc mkdir "Main_Results/Fig3/"

capture confirm file "Main_Results/Fig3/gphs/"
if _rc mkdir "Main_Results/Fig3/gphs/"



use "Data/Big_Pandemics.dta", clear
gen pandemic=1
drop year
ren endyear year
sort year
merge 1:1 year using "Data/UKGDP.dta"
sort year
merge 1:1 year using "Data/RealRateSince1310.dta", nogen
sort year
replace pandemic=0 if pandemic==.
gen pandemic100k=cond(dead>=100000,pandemic,0)

sort year


keep year gdppc pandemic100k UKR_real 
sort year


tsset year
replace gdppc = 100*log(gdppc)
gen gdpgrowth = d.gdppc

cap drop b0* se0* b1* se1*  Horizon
cap drop h0* h1* l0* l1*
	gen Horizon = _n-1 if _n<=41
	gen b0=0 if Horizon==0
	gen se0=0 if Horizon==0
	gen h0=0 if Horizon==0
	gen h1=0 if Horizon==0
	gen l0=0 if Horizon==0
	gen l1=0 if Horizon==0

sort  year
tsset year


cap drop raw
gen raw = .
replace raw = gdpgrowth


/********** State space specification starts here *************/

constraint 1	[gdpgrowthstar]L.gdpgrowthstar		= 1		/* Random Walk */	
constraint 2	[raw]gdpgrowthstar			= 1		/* Definitional */


cap drop zero								/* generate zero line for figures */
gen zero = 0

tsset year
cap drop gdpgrowth_star

/************	 State space equations	 ******************/

sspace		(gdpgrowthstar L.gdpgrowthstar, state noconstant)		/// /* r-star */
			(raw gdpgrowthstar, noconstant ),				/// /* raw */
			covstate(identity) constraints(1/2) difficult /*method(kdiffuse)*/ iterate(25)
			
			qui{
			/* Generate smoothed estimates of r-star */
			predict gdpgrowth_star if e(sample), states smethod(smooth) equation(gdpgrowthstar)
			
		
		
			cap drop trend_gdpgrowth
			gen	trend_gdpgrowth = gdpgrowth_star
			}

				
cap drop raw
gen raw = .
replace raw = UKR_real
/********** State space specification starts here *************/

constraint 1	[rstar]L.rstar		= 1		/* Random Walk */	
constraint 2	[raw]rstar			= 1		/* Definitional */


cap drop zero								/* generate zero line for figures */
gen zero = 0

tsset year
cap drop r_star

/************	 State space equations	 ******************/

sspace		(rstar L.rstar, state noconstant)		/// /* r-star */
			(raw rstar, noconstant ),				/// /* raw */
			covstate(identity) constraints(1/2) difficult /*method(kdiffuse)*/ iterate(25)
			
			qui{
			/* Generate smoothed estimates of r-star */
			predict r_star if e(sample), states smethod(smooth) equation(rstar)
				
			
		
		
			cap drop trend_rstar
			gen	trend_rstar = r_star

			}



gen rminusg = trend_rstar - trend_gdpgrowth  

gen time = year
eststo clear
forv h=0/40 {
gen drminusg`h' = f`h'.rminusg-l.rminusg
reg drminusg`h' pandemic100k  l(1/10).pandemic100k   l(2/11).rminusg 
eststo ols`h'
	replace b0  = _b[pandemic100k]                      if Horizon==`h'
	replace se0 = _se[pandemic100k]                     if Horizon==`h'
	replace h0  = _b[pandemic100k]+2*_se[pandemic100k]  if Horizon==`h'
	replace l0  = _b[pandemic100k]-2*_se[pandemic100k]  if Horizon==`h'
	replace h1  = _b[pandemic100k]+1*_se[pandemic100k]  if Horizon==`h'
	replace l1  = _b[pandemic100k]-1*_se[pandemic100k]  if Horizon==`h'
	
}
esttab , keep(pandemic*)
	cap drop Zero
	gen Zero=0
	twoway ///
	(  rarea h0 l0 Horizon if Horizon~=. , fc(blue)  fi(10) lcolor(white)  lpattern(none)  )  ///
		(  rarea h1 l1 Horizon if Horizon~=. , fc(blue)  fi(20) lcolor(white) lw(none)  lpattern(none)  )  ///
		(  line  Zero    Horizon if Horizon~=. , lc(gray)  lw(med) lp(dash)                     )  ///
		(  line  b0     Horizon if Horizon~=. , lc(blue)  lw(thick) lp(solid)  ///
				xlab(0(10)40, labsize(large)) ylab(-1.5(0.5)1, angle(0) labsize(large) nogrid) xtitle("Years since pandemic event ended", size(large)) ///
				ytitle("Percent", size(large))) ///
		, title()    ///
		xsize(5) ysize(4) scale(1.1) ysc(r(-1.5 1))  scheme(s1color)   ///
		legend(off ) ///
		 name(UKrminusg_wars,replace) /// nodraw

	
graph save   Main_Results/Fig3/gphs/Fig3g.gph, replace
graph export Main_Results/Fig3/Fig3g.pdf, replace



